<template>
  <div class="container">
    <div class="search-box">
      <el-input
        placeholder="请输入内容"
        class="input-box"
        v-model="value"
        @input="reset"
      ></el-input>
      <el-button @click="eliminate">清除</el-button>
      <el-button type="primary" @click="search">搜索</el-button>
    </div>
    <el-table border style="width: 100%" :data="list">
      <el-table-column :index="indexAdd" type="index" label="任务编号" width="80">
      </el-table-column>
      <el-table-column prop="name" label="任务名称"> </el-table-column>
      <el-table-column prop="des" label="任务描述"> </el-table-column>
      <el-table-column fixed="right" label="操作" width="180">
        <template #default="{ row }">
          <el-button size="mini" style="margin-right: 10px" @click="edittask(row.id)"
            >编辑</el-button
          >
          <el-popconfirm
            confirmButtonText="好的"
            cancelButtonText="不用了"
            icon="el-icon-info"
            iconColor="red"
            title="确定删除任务吗？"
            @confirm="deltask(row.id)"
          >
            <template #reference>
              <el-button size="mini" type="danger">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div class="pageContainer">
      <el-pagination
        :total="total"
        :current-page="params._page"
        :page-sizes="[2, 3, 5, 7, 10]"
        :page-size="params._limit"
        layout="total,sizes,prev,pager,next,jumper"
        @current-change="pageChange"
        @size-change="pageSizeChange"
      />
      <el-button size="small" plain @click="addtask">+ 新增</el-button>
    </div>
    <el-dialog title="提示" :visible="dialogVisible" width="30%" @close="closeDialog">
      <template>
        <el-form ref="Form" label-width="80px" :model="form" :rules="rules">
          <el-form-item label="任务名称" prop="name">
            <el-input v-model="form.name"></el-input>
          </el-form-item>
          <el-form-item label="任务名称" prop="des">
            <el-input v-model="form.des"></el-input>
          </el-form-item>
        </el-form>
      </template>
      <template #footer>
        <span>
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="confirm">确 定</el-button>
        </span>
      </template>
    </el-dialog>
    <el-dialog title="提示" :visible="showDialog" width="30%" @close="closeDialogs">
      <template>
        <el-form ref="editForm" label-width="80px" :model="editform" :rules="rules">
          <el-form-item label="任务名称" prop="name">
            <el-input v-model="editform.name"></el-input>
          </el-form-item>
          <el-form-item label="任务名称" prop="des">
            <el-input v-model="editform.des"></el-input>
          </el-form-item>
        </el-form>
      </template>
      <template #footer>
        <span>
          <el-button @click="cancel">取 消</el-button>
          <el-button type="primary" @click="editconfirm">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { v4 as uuidv4 } from 'uuid'
import {
  fetchList,
  fetchaddTask,
  fetcheditTask,
  fetchdelTask,
  editsTask,
  searchTask
} from '@/api/programmer'
export default {
  data() {
    // const validatePass = (rule, value, callback) => {
    //   if (value == this.editform.name) {
    //     callback(new Error('和原任务名称重复'))
    //   } else {
    //     callback()
    //   }
    // }
    return {
      value: '',
      dialogVisible: false,
      showDialog: false,
      list: [],
      total: null,
      params: {
        _page: 1, // 当前第几页
        _limit: 5 // 每页条数
      },
      form: {
        name: '',
        des: '',
        id: uuidv4()
      },
      editform: {
        name: '',
        des: '',
        id: uuidv4()
      },
      rules: {
        name: [
          { required: true, message: '任务名称必填', trigger: 'blur' },
          { min: 2, max: 5, message: '长度2~5位', trigger: 'blur' }
        ],
        des: [
          { required: true, message: '任务描述必填', trigger: 'blur' },
          { min: 2, max: 10, message: '长度2~10位', trigger: 'blur' }
        ]
      }
      // rulesedit: {
      //   name: [
      //     { required: true, message: '任务名称必填', trigger: 'blur' },
      //     { min: 2, max: 10, message: '长度2~10位', trigger: 'blur' },
      //     { validator: validatePass, trigger: 'blur' }
      //   ],
      //   des: [
      //     { required: true, message: '任务描述必填', trigger: 'blur' },
      //     { min: 2, max: 10, message: '长度2~10位', trigger: 'blur' }
      //   ]
      // }
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    pageSizeChange(page) {
      this.params._limit = page
      this.getList()
    },
    pageChange(page) {
      this.params._page = page
      this.getList()
    },
    indexAdd(index) {
      return index + 1 + (this.params._page - 1) * this.params._limit
    },
    async getList() {
      const res = await fetchList(this.params)
      // console.log(res)
      this.list = res.data
      this.total = Number(res.headers['x-total-count'])
    },
    closeDialog() {
      this.dialogVisible = false
    },
    addtask() {
      this.dialogVisible = true
    },
    confirm() {
      this.$refs.Form.validate(async valid => {
        if (valid) {
          await fetchaddTask(this.form)
          this.$message.success('新增任务成功')
          this.$refs.Form.resetFields()
          this.getList()
          this.dialogVisible = false
        } else {
          this.$message.error('新增任务失败')
        }
      })
    },
    async deltask(id) {
      await fetchdelTask(id)
      this.$message.success('删除任务成功')
      this.getList()
    },
    async edittask(id) {
      this.showDialog = true
      const res = await fetcheditTask(id)
      this.editform = res.data
    },
    editconfirm() {
      this.$refs.editForm.validate(async valid => {
        if (valid) {
          await editsTask(this.editform)
          this.$message.success('编辑任务成功')
          this.$refs.editForm.resetFields()
          this.getList()
          this.showDialog = false
        } else {
          this.$message.error('编辑任务失败')
        }
      })
    },
    async search() {
      const res = await searchTask(this.value)
      this.list = res.data
    },
    eliminate() {
      this.value = ''
      this.getList()
    },
    cancel() {
      this.showDialog = false
      this.$refs.editForm.resetFields()
    },
    closeDialogs() {
      this.showDialog = false
    },
    reset() {
      if (this.value === '') {
        this.params._page = 1
        this.getList()
      }
    }
  }
}
</script>

<style scoped lang="less">
.container {
  width: 800px;
  margin: 0 auto;
}
.search-box {
  display: flex;
  margin-bottom: 10px;
  .input-box {
    margin-right: 20px;
  }
}
.pageContainer {
  margin-top: 10px;
  display: flex;
  justify-content: space-between;
}
</style>
